package problems;

import lib.MathLib;

public class Euler046 extends AbstractEuler {

	@Override
	public Number calculate() {
		//the conjecture to be proven false is that any odd composite can be written as the sum of a prime and twice a square.
		//In formula form: oddComposite = a + 2b^2
		
		//to disprove it, try for increasingly large numbers of oddComposite, for each 2b^2 smaller than or equal to oddComposite - 2,
		//whether their difference (a) is prime. If none can be found, we have our answer.
		
		nextCandidate:
		for (int oddComposite = 9; oddComposite < Math.sqrt(Integer.MAX_VALUE) / 2; oddComposite += 2) {
			if (MathLib.isPrime(oddComposite)) continue nextCandidate; //primes aren't composite
			for (int b = 1, twiceBSquared = 2; twiceBSquared - 2 <= oddComposite; b++, twiceBSquared = (int)(2 * Math.pow(b, 2))) {
				if (MathLib.isPrime(oddComposite -  twiceBSquared)) continue nextCandidate;
			}
			return oddComposite;
		}
		
		fail("failed to disprove conjecture without exceeding the limits of Integer.MAX_VALUE");
		return null;
	}

	@Override
	protected Number getCorrectAnswer() {
		return 5777;
	}

}
